package dao;

// Generated 2 juin 2012 17:41:31 by Hibernate Tools 3.4.0.CR1

import java.util.List;
import javax.naming.InitialContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.SessionFactory;

import dao.interfaces.IdaoBagage;

import beans.Bagage;
import static org.hibernate.criterion.Example.create;

/**
 * Home object for domain model class Bagage.
 * @see beans.Bagage
 * @author Hibernate Tools
 */
public class DaoBagage implements IdaoBagage {

	private static final Log log = LogFactory.getLog(DaoBagage.class);

	private final SessionFactory sessionFactory = getSessionFactory();

	protected SessionFactory getSessionFactory() {
		try {
			System.out.println("session");
			return HibernateUtil.getSessionFactory();
		} catch (Exception e) {
			log.error("Could not locate SessionFactory in JNDI", e);
			throw new IllegalStateException(
					"Could not locate SessionFactory in JNDI");
		}
	}

	/* (non-Javadoc)
	 * @see dao.IdaoBagage#persist(beans.Bagage)
	 */
	@Override
	public void persist(Bagage transientInstance) {
		log.debug("persisting Bagage instance");
		try {
			sessionFactory.getCurrentSession().persist(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void attachDirty(Bagage instance) {
		log.debug("attaching dirty Bagage instance");
		try {
			sessionFactory.getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Bagage instance) {
		log.debug("attaching clean Bagage instance");
		try {
			sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see dao.IdaoBagage#delete(beans.Bagage)
	 */
	@Override
	public void delete(Bagage persistentInstance) {
		log.debug("deleting Bagage instance");
		try {
			sessionFactory.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see dao.IdaoBagage#merge(beans.Bagage)
	 */
	@Override
	public Bagage merge(Bagage detachedInstance) {
		log.debug("merging Bagage instance");
		try {
			Bagage result = (Bagage) sessionFactory.getCurrentSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see dao.IdaoBagage#findById(java.lang.String)
	 */
	@Override
	public Bagage findById(java.lang.String id) {
		log.debug("getting Bagage instance with id: " + id);
		try {
			Bagage instance = (Bagage) sessionFactory.getCurrentSession().get(
					"beans.Bagage", id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Bagage> findByExample(Bagage instance) {
		log.debug("finding Bagage instance by example");
		try {
			List<Bagage> results = (List<Bagage>) sessionFactory
					.getCurrentSession().createCriteria("beans.Bagage")
					.add(create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}
}
